import time
import os
from common.save_records import SaveFile
from pages.qvc import Qvc


def spider_hsn(excel_file_name, amazon_product_url):
    print("Task:%s..is staring" % excel_file_name)
    create_time = time.strftime('%Y-%m-%d-%H-%M', time.localtime(time.time()))
    excel_file_full_path_name = os.path.join("D:\\pycode\\JT_spider\\log", 'grandinroad_%s_%s.xlsx' % (excel_file_name,create_time))
    produce_img_dir_path = "D:\\pycode\\JT_spider\\log\\img"

    save_files = SaveFile(excel_file_full_path_name, produce_img_dir_path)
    excel_file, excel_file_format, file_object = save_files.create_excel()
    if not os.path.exists(produce_img_dir_path):
        os.mkdir(produce_img_dir_path)

    spider = Qvc(amazon_product_url)

    row_num = 1
    for info in spider.get_info_from_page():
        img_path = save_files.download_img(info[0])
        excel_file.set_row(row_num, 250)
        excel_file.insert_image(row_num, 0, img_path)
        for j in range(1, len(info)):
            excel_file.write(row_num, j, info[j], excel_file_format)
        row_num += 1
        break
    file_object.close()


if __name__ == "__main__":
    start_time = time.time()
    info_dic = dict()
    excel_file_name1 = "allplay-free-shipping"
    web_url1 = "https://www.qvc.com/content/listing.html?qn=allplay-free-shipping&pt=9u%2FIcGQGYcIj7m14SWzpvw%3D%3D"

    spider_hsn(amazon_product_url=web_url1, excel_file_name=excel_file_name1)
    print("Spend time: %s" %(time.time() - start_time))

